package s63.kong.shop.modules.login;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import s63.kong.shop.modules.security.LoginUser;
import s63.kong.shop.modules.token.ITokenService;
import s63.kong.shop.modules.token.Token;

import javax.annotation.Resource;

@Service
public class LoginService {
    @Resource
    private AuthenticationManager authenticationManager;//这里调用了SecurityConfig配置文件中的Bean对象
    @Resource
    private ITokenService tokenService;

    public Token login(LoginVo loginVo) {
        UsernamePasswordAuthenticationToken authenticationToken
                = new UsernamePasswordAuthenticationToken(loginVo.getUsername(),loginVo.getPassword());
        Authentication authentication = authenticationManager.authenticate(authenticationToken);
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return tokenService.createToken(loginUser);
    }
}
